import pytest
import requests
from Config.Config1 import Config
from Tools.Tools import *
from Tools.db import db
@pytest.fixture
def db_database():
    sjk=db(host='10.59.9.12',user='root',password='yidong666',database='a3mall')
    yield sjk
    sjk.close()
data=read_yaml(f'D:\Gao6x\Auto_Wms\Lx_yk\data\data.yaml')
class Test_case:
    token=None
    spid=None
    zsxl_id=None
    order_id=None
    @pytest.fixture(scope='session',autouse=True)
    def config(self):
        return Config('http://10.59.9.12:7001')
    @pytest.mark.parametrize('data',data)
    # 登录
    def test_login(self,config,data,db_database):
        try:
            body={"username":data['username'],"password":data['password']}
            login=config.post(path='/api/public/login',json=body)
            print(login.json())
            sql=db_database.select("select username from mall_users where username='13811033962'")[0]
            assert data['username']==sql[0]
            print("打印数据库数据：", sql[0])
            Test_case.token='Bearer '+login.json()['data']['token']
            print('获取的token为:',Test_case.token)
        except Exception as e:
            print(e)
    # 查看登录信息
    def test_info(self,config):
        head={'auth-token':Test_case.token}
        info=config.get(path='/api/ucenter/info',headers=head)
        print(info.json())
    # 查看我的积分
    def test_jifen(self,config):
        head = {'auth-token': Test_case.token}
        body={"page":"1"}
        info=config.get(path='/api/ucenter/point?page=1',headers=head,json=body)
        print(info.json())

        # 查看收货地址列表
    def test_shlist(self,config):
        head = {'auth-token': Test_case.token}
        info = config.get(path='/api/ucenter/address/list', headers=head)
        print(info.json())

    # 查看购物车列表
    def test_gwclist(self,config):
        head = {'auth-token': Test_case.token}
        body = {"page": "1"}
        info = config.get(path='/api/cart?page=1', headers=head,json=body)
        print(info.json())
#         提取商品id大米
        spid=info.json()['data']['list'][0]['id']
        print(spid)

    # 删除购物车商品
    def test_deletesp(self,config):
        head = {'auth-token': Test_case.token}
        body={"id":str(Test_case.spid)}
        info = config.post(path='/api/cart/delete', headers=head,json=body)
        print(info.json())

    # 首页搜索音乐耳机
    def test_cha1(self,config):
        head = {'auth-token': Test_case.token}
        body={"keywords":"音乐耳机"}
        info=config.get(path='/api/search/list?keywords=音乐耳机',headers=head,json=body)
        print(info.json())

        # 商品详情
    def test_spxq(self, config):
        head = {'auth-token': Test_case.token}
        info = config.get(path='/api/goods/view?id=94', headers=head)
        print(info.json())
        # zsxl_id=info.json()['data']['goods']['id']
        # print('商品详情:',zsxl_id)

        # 添加至购物车
    def test_add(self, config):
        head = {'auth-token': Test_case.token}
        body = {"id":94,"sku_id":"","num":1}
        info = config.post(path='/api/cart/add', headers=head, data=body)
        print(info.json())
    # 查询购物车
    def test_chagwc(self,config):
        head = {'auth-token': Test_case.token}
        info = config.get(path='/api/cart?page=1', headers=head)
        print(info.json())
        zsxl_id=info.json()['data']['list'][0]['id']
        print(zsxl_id)
    # 生成商品订单
    def test_addgwc(self, config):
        head = {'auth-token': Test_case.token}
        body = {"id":2206,"type":"cart","shipping_type":"1"}
        info = config.get(path=f'/api/order/confirm?id=2206&type=cart&shipping_type=1', headers=head, json=body)
        print(info.json())


    # 支付商品
    def test_pay(self, config):
        head = {'auth-token': Test_case.token}
        body = {"id":Test_case.zsxl_id,"type":"cart","address_id":343,"store_id":2,"shipping_type":1,"bonus_id":"0","payment":"balance","remarks":"","source":1,"url":"http://10.59.9.12:7001/wap/pages/cart/confirm?id=2190&type=cart"}
        info = config.post(path='/api/order/create', headers=head, json=body)
        print(info.json())
        # order_id=info.json()['data']['order_id']
        # print(order_id)



 # # 确认收货
 #    def test_qrsh(self, config):
 #        head = {'auth-token': Test_case.token}
 #        body = {"id":"64767"}
 #        info = config.post(path='/api/order/confirm_delivery', headers=head, data=body)
 #        print(info.json())

